<?php

/**
 * String manipulations.
 * @package Util
 * @author bartu
 */
class SafeStrings
{
	/**
	 * remove special characters from the string
	 *
	 * @param  string $string
	 * @return string
	 */
	public static function createAlias ($string)
	{
	    $chars_from = array("ö", "ü", "ó", "ő", "ú", "é", "á", "ű", "í", "Ö", "Ü", "Ó", "Ő", "Ú", "É", "Á", "Ű", "Í");
	    $chars_to   = array("o", "u", "o", "o", "u", "e", "a", "u", "i", "O", "U", "O", "O", "U", "E", "A", "U", "I");
	    $string     = str_replace($chars_from, $chars_to, $string);
	    $string     = preg_replace("/[^a-z0-9_-]/i", "_", $string);

	    do {
	        $string = str_replace("__", "_", $string);
	    } while (ereg("__", $string));

	    $string = trim($string, "_");

	    return strtolower($string);
	}

	/**
	 * remove < and > characters from the given string
	 * @param  string $text
	 * @return string
	 */
	public static function removeHtml ($text)
	{
	    $text = str_replace("<", "&lt;", $text);
	    $text = str_replace(">", "&gt;", $text);

	    return $text;
	}

	/**
	 * create safe filname without special chars etc.
	 *
	 * @param  string $fileName
	 * @return string
	 */
	public static function safeFileName ($fileName)
	{
	    $ext  = substr(strrchr($fileName, "."), 1);
	    $name = substr($fileName, 0, -strlen($ext));

	    return self::CreateAlias($name).".".$ext;
	}

	/**
	 * Ellenorzi, hogy egy adott karaktersor csak ekezetmentes karaktereket,
	 * es szamokat illetve _ - karaktereket tartalmaz-e
	 * @param  string
	 * @return int (1, 0)
	 */
	public static function isSafeString ($string)
	{
	    return (ereg("^[0-9A-Za-z_-]+$", $string)) ? 1 : 0;
	}
}

?>